import {Component, OnInit, Input, Output, EventEmitter} from '@angular/core';
import {fadeIn} from "../../animate";
import {UtilService} from "../../my-service/util.service";
import {TeamManagerService} from "../../my-service/teamManager.service";
import {NzMessageService} from 'ng-zorro-antd';

@Component({
  selector: 'app-add-accredit',
  templateUrl: './add-accredit.component.html',
  styleUrls: ['./add-accredit.component.css'],
  animations: [fadeIn]
})
export class AddAccreditComponent implements OnInit {
  @Output() onTeam = new EventEmitter();
  @Input() team: any;
  userName = null;
  userPsd = null;
  currentIndex = null; // 当前检索出来的用户索引
  isComfirm = false; // 当前用户是否进行确认授权操作
  isExit = false;
  readImgUrl = '';
  defaultImg = '';
  currentSearchUser = {
    UserName: '',
    Cover: '',
    Name: '',
    PK_UserGuid: '',
  };

  constructor(private utilServe: UtilService,
              private teamServe: TeamManagerService,
              private message: NzMessageService) {
    this.readImgUrl = this.utilServe.readImgUrl;
    this.defaultImg = this.utilServe.defaultImg;
  }

  ngOnInit() {
  }

  searchUser() {
    this.isExit = false;
    if (this.team.limitNumber > this.team.haveAccreditMembers.length + this.team.noAccreditMembers.length) {
      if (!this.userName || !this.userPsd) {
        this.message.error('账号或者密码不能为空');
        return;
      } else {
        let data = {
          SystemName: '创新者管理系统',
          Name: this.userName,
          Password: this.userPsd
        };
        this.teamServe.getUserGuid(data).then(res => {
          if (res.status === 1) {
            this.currentSearchUser.PK_UserGuid = res.UserGuid;
            let isSearch = false; // 是否有检索到团队内的用户
            this.team.noAccreditMembers.forEach((member, index) => {
              if (member.PK_UserGuid === this.currentSearchUser.PK_UserGuid) {
                this.currentSearchUser = member;
                this.currentIndex = index;
                isSearch = true;
              }
            });
            // 判断当前检索用户是否已经授权
            this.team.haveAccreditMembers.forEach((member, index) => {
              if (member.PK_UserGuid === this.currentSearchUser.PK_UserGuid) {
                this.isExit = true;
                this.message.error('当前用户已经授权，请重新选择用户授权');
                return;
              }
            });
            if (!isSearch && !this.isExit) {
              // 没有加入团队，先加入团队再授权
              this.teamServe.addMembers(this.team.teamGuid, JSON.stringify({
                FK_userGuid: this.currentSearchUser.PK_UserGuid,
                isAccredit: false
              })).then(res => {
                // 根据用户Guid获取用户信息
                this.teamServe.getUserInfo(this.currentSearchUser.PK_UserGuid).then(res => {
                  this.currentSearchUser = res.data;
                  this.team.noAccreditMembers.push(this.currentSearchUser);
                  this.currentIndex = this.team.noAccreditMembers.length - 1;
                });
              });
            }
          } else {

            this.message.error(res.message);
          }
        })
      }
    } else {
      console.log(this.team);
      this.message.error('当前团队能够授权或加入人数已经达到上限');
    }

  }

  onClose() {
    this.team.openAccredit = false;
    if (this.isComfirm) {
      this.onTeam.emit(this.team);
    }
  }

  affirm() {
    if (this.isExit) {
      this.message.error('当前用户已经授权，请重新选择用户授权');
    } else {
      if (this.currentSearchUser.PK_UserGuid) {
        this.utilServe.comfirm('您确认要对该用户进行授权嘛？', () => {
          let data = {
            FK_teamGuid: this.team.teamGuid,
            FK_userGuid: this.currentSearchUser.PK_UserGuid
          };
          this.teamServe.addAccredit(data).then(res => {
            if (res.status == 1) {
              this.team.haveAccreditMembers.push(this.currentSearchUser);
              this.team.noAccreditMembers.splice(this.currentIndex, 1);
              this.isComfirm = true;
              this.isExit = false;
              this.message.success('团队授权成功');
              this.currentSearchUser = {
                UserName: '',
                Cover: '',
                Name: '',
                PK_UserGuid: '',
              };
            }
          })

        })
      } else {
        this.message.error('您当前未选择授权用户，请先选择授权用户再进行授权');
        return;
      }
    }
  } // 确认授权
}
